﻿/**
 * Form used for creating and editing Consolidations
 */
Ext.define('Sisconsolid.view.consolidation.edit.Form', {
    extend: 'Ext.form.Panel',
    alias: 'widget.consolidation.edit.form',
    requires: [
        'Ext.form.FieldSet',
		'Ext.form.FieldContainer',
        'Ext.form.field.Date',
        'Ext.form.field.Text',
		'Ext.form.field.ComboBox',
        'Sisconsolid.ux.form.field.RemoteComboBox',
		'Sisconsolid.store.util.CivilStatus',
		'Sisconsolid.store.address.Provinces',
		'Sisconsolid.store.address.Cities',
		'Sisconsolid.store.address.Neighborhoods',
		'Sisconsolid.store.address.Streets',
		'Sisconsolid.store.util.ConsolidatorList',
		'Sisconsolid.store.util.CellList',
		'Sisconsolid.store.Services',
		'Sisconsolid.store.Nets'
    ],
	bodyPadding: 10,
    initComponent: function() {
        var me = this;
        Ext.applyIf(me, {
            fieldDefaults: {
                allowBlank: false,
                labelAlign: 'top',	
                flex: 1,
				margins: 3
            },
            defaults: {
                layout: 'hbox'
            },
            items: [
                {
                    xtype: 'fieldset',
					title: translations.personal,
					layout: 'anchor',
					defaults: {
						layout: '100%'
					},
                    items: [
						{
							xtype: 'fieldcontainer',
							layout: 'hbox',
							items: [
								{
									xtype: 'textfield',
									name: 'firstName',
									fieldLabel: translations.firstName,
									maxLength: 100,
									flex: 4
								},
								{
									xtype: 'textfield',
									name: 'lastName',
									fieldLabel: translations.lastName,
									maxLength: 100,
									flex: 4
								},
								{
									xtype: 'numberfield',
									name: 'years',									
									fieldLabel: translations.years,
									minValue: 0,
									maxLength: 3,
									hideTrigger: true,
									keyNavEnabled: false,
									mouseWheelEnabled: false,
									flex: 1
								},
								{								
									xtype: 'radiogroup',									
									fieldLabel: translations.sex,
									flex: 1.5,									
									items: [
										{boxLabel: 'M', name: 'sex', inputValue: 'M'},
										{boxLabel: 'F', name: 'sex', inputValue: 'F'}
									]
								},
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'civilStatus',
									fieldLabel: translations.civilStatus,
									store: {
										type: 'util.civilstatus'
									},
									valueField: 'key',									
									displayField: 'value',									
									editable: false,
									forceSelection: true,
									flex: 2
								},
								{
									xtype: 'textfield',
									name: 'birthday',
									fieldLabel: translations.birthday,
									regex: /(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[012])/,
									flex: 1
									
								}
							]
						},
						{
							xtype: 'fieldcontainer',
							layout: 'hbox',
							items: [
								{
									xtype: 'textfield',
									name: 'occupation',
									fieldLabel: translations.occupation,
									flex: 1.75
								},
								{
									xtype: 'textfield',
									name: 'email',
									fieldLabel: translations.email,
									maxLength: 100,
									regex:/^((([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z\s?]{2,5}){1,25})*(\s*?;\s*?)*)*$/,
									regexText: translations.invalidEmail,
									flex: 2
								},
								{
									xtype: 'textfield',
									name: 'contactTime',
									fieldLabel: translations.contactTime,
									maxLength: 100,
									flex: 3
								},
								{
									xtype: 'textfield',
									name: 'personalPhone',
									fieldLabel: translations.personalPhone,
									allowBlank: true,
									maxLength: 30,
									flex: 1.75
								},
								{
									xtype: 'textfield',
									name: 'workPhone',
									fieldLabel: translations.workPhone,
									allowBlank: true,
									maxLength: 30,
									flex: 1.75
								},
								{
									xtype: 'textfield',
									name: 'visitingDay',
									fieldLabel: translations.visitingDay,
									allowBlank: true,
									maxLength: 100,
									flex: 2
								}
							]
						}
                    ]
                },
                {
                    xtype: 'fieldset',
					title: translations.address,
					layout: 'anchor',
					defaults: {
						layout: '100%'
					},
                    items: [
						{
							xtype: 'fieldcontainer',
							layout: 'hbox',
							items: [
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idProvince',
									fieldLabel: translations.province,
									store: {
										type: 'address.province'
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true,
								},
								{
									xtype: 'combobox',
									name: 'idCity',
									fieldLabel: translations.city,
									store: {
										type: 'address.city',
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true
								},
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idNeighborhood',
									fieldLabel: translations.neighborhood,
									store: {
										type: 'address.neighborhood'
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true,
									labelAlign: 'top',
								},
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idStreet',
									fieldLabel: translations.street,
									store: {
										type: 'address.street'
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true,
									labelAlign: 'top',
								}
							]
						},
						{
							xtype: 'fieldcontainer',
							layout: 'hbox',
							items: [
								{
									xtype: 'fieldcontainer',
									layout: 'hbox',
									items: [
										{
											xtype: 'numberfield',
											name: 'number',
											fieldLabel: translations.number,
											minValue: 0,
											maxLength: 8,									
											allowBlank: true,
											hideTrigger: true,
											keyNavEnabled: false,
											mouseWheelEnabled: false,
											flex: 3,
										}, 
										{
											xtype: 'hiddenfield',
											flex: 5									
										}
									]
								},
								{
									xtype: 'fieldcontainer',
									layout: 'hbox',
									items: [
										{
											xtype: 'textfield',
											name: 'floor',
											fieldLabel: translations.floor,
											allowBlank: true,
											maxLength: 5,
											flex: 1
										},
										{
											xtype: 'textfield',
											name: 'department',
											fieldLabel: translations.department,
											allowBlank: true,
											maxLength: 5,
											flex: 1
										}, 
										{
											xtype: 'hiddenfield',
											flex: 2									
										}
									]
								},
								{
									xtype: 'fieldcontainer',
									layout: 'hbox',
									items: [
										{	
											xtype: 'textfield',
											name: 'block',
											fieldLabel: translations.block,
											allowBlank: true,
											maxLength: 5,
											flex: 1
										},
										{
											xtype: 'textfield',
											name: 'lot',
											fieldLabel: translations.lot,
											allowBlank: true,
											maxLength: 5,
											flex: 1
										}, 
										{
											xtype: 'hiddenfield',
											flex: 2									
										}
									]
								}, 
								{
									xtype: 'hiddenfield'					
								}
							]
						}
                    ]
                },
                {
                    xtype: 'fieldset',
					title: translations.consolidation,
					layout: 'anchor',
					defaults: {
						layout: '100%'
					},
                    items: [
						{
							xtype: 'fieldcontainer',
							layout: 'hbox',
							items: [
								{
									xtype: 'datefield',
									name: 'date',
									fieldLabel: translations.date,
									submitFormat: 'Y-m-d',
									maxValue: new Date(),
									flex: 2									
								},
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idService',
									fieldLabel: translations.service,
									store: {
										type: 'service'
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true,
									flex: 4
								},
								{
									xtype: 'textareafield',
									anchor: '100%',
									name: 'need',
									fieldLabel: translations.need,
									maxLength: 500,
									height: 80,
									flex: 5									
								},
								{
									xtype: 'textareafield',
									anchor: '100%',
									name: 'observation',
									fieldLabel: translations.observation,
									allowBlank: true,
									maxLength: 500,
									height: 80,
									flex: 5
								}
							]
						},
						{
							xtype: 'fieldcontainer',
							layout: 'hbox',
							items: [
								{								
									xtype: 'radiogroup',									
									fieldLabel: translations.attendedChurch,
									name: 'attendedChurch',
									setValue : function(myValue){                        
										var me = this;
										me.batchChanges(function() {
											me.eachBox(function(cb) {
											   cb.setValue(myValue == cb.inputValue);
											});
										});
									},
									items: [
										{
											inputValue: true,
											boxLabel: translations.yes,
											name: 'attendedChurch'
										}, {
											inputValue: false,
											boxLabel: translations.no,
											name: 'attendedChurch'
										}
									],
									flex: 2
								}, 
								{
									xtype: 'hiddenfield',
									id: 'idWhichChurchSpace',									
									flex: 4									
								},
								{
									xtype: 'textfield',
									name: 'whichChurch',
									fieldLabel: translations.whichChurch,
									maxLength: 150,
									hidden: true,
									flex: 4
								},
								{
									xtype: 'textfield',
									name: 'invitedBy',
									fieldLabel: translations.invitedBy,
									maxLength: 150,
									flex: 5
								},
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idTookData',
									fieldLabel: translations.tookData,
									store: {
										type: 'util.consolidatorList'
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true,
									flex: 5
								}
							]
						},
						{
							xtype: 'fieldcontainer',
							layout: 'hbox',
							items: [
								{								
									xtype: 'radiogroup',									
									fieldLabel: translations.attendCell,
									name: 'attendCell',
									setValue : function(myValue){                        
										var me = this;
										me.batchChanges(function() {
											me.eachBox(function(cb) {
											   cb.setValue(myValue == cb.inputValue);
											});
										});
									},
									items: [
										{
											inputValue: true,
											boxLabel: translations.yes,
											name: 'attendCell'
										}, {
											inputValue: false,
											boxLabel: translations.no,
											name: 'attendCell'
										}
									],
									flex: 2
								}, 
								{
									xtype: 'hiddenfield',
									id: 'idWhichCellSpace',									
									flex: 4									
								},								
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idCell',
									fieldLabel: translations.whichCell,
									store: {
										type: 'util.cellList'
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true,
									flex: 4
								},
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idNet',
									fieldLabel: translations.net,
									store: {
										type: 'net'
									},
									valueField: 'key',									
									displayField: 'value',									
									editable: false,
									forceSelection: true,
									flex: 5
								},
								{
									xtype: 'ux.form.field.remotecombobox',
									name: 'idAssignedTo',
									fieldLabel: translations.assignedTo,
									store: {
										type: 'util.consolidatorList'
									},
									valueField: 'key',									
									displayField: 'value',
									editable: false,
									forceSelection: true,
									flex: 5
								}
							]
						}
                    ]
                }
            ]
        });
        me.callParent( arguments );
    }
});